package org.example.library.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class ResponseUtil {
        public static void genBaseResponse(HttpServletResponse resp,int code,String msg,Object data) throws IOException {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",code);
            jsonObject.put("msg",msg);
            jsonObject.put("data",data);
            resp.getWriter().write(JSON.toJSONString(jsonObject));
        }
    public static void gen404Response(HttpServletResponse resp, String msg) throws IOException {
        genBaseResponse(resp, 404, msg, null);
    }
    public static void gen500Response(HttpServletResponse resp, String msg) throws IOException {
        genBaseResponse(resp, 500, msg, null);
    }
    public static void genSuccessResponse(HttpServletResponse resp, String msg, Object data) throws IOException {
        genBaseResponse(resp, 0, msg, data);
    }
}
